package com.zsl.swing.redis.desktop.window.help;

import com.zsl.swing.redis.desktop.common.Constants;
import com.zsl.swing.redis.desktop.common.IconPaths;
import com.zsl.swing.redis.desktop.model.FontSizeConfigEntity;
import com.zsl.swing.redis.desktop.type.BtnOptEnum;
import com.zsl.swing.redis.desktop.utils.CommonUtils;
import com.zsl.swing.redis.desktop.utils.FontUtils;
import com.zsl.swing.redis.desktop.utils.IconUtils;
import com.zsl.swing.redis.desktop.window.ZslRedisDesktopMainWindow;
import com.zsl.swing.redis.desktop.window.builder.RedisCommandGroupComboBoxBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.URI;

public class ZslRedisCmdHelpWindow extends JFrame {

    private static final int W = 1400;

    private static final int H = 600;

    /**
     * 官网URL
     */
    private static final String COMMAND_URL = "https://redis.io/commands/";


    /**
     * 字体设置
     */
    private FontSizeConfigEntity fontConfig = ZslRedisDesktopMainWindow.getFontSizeConfig();

    /**
     * 关键字
     */
    private JTextField keywordField = new JTextField(20);

    /**
     * 分组
     */
    private JComboBox<String> groupBox = RedisCommandGroupComboBoxBuilder.getInstance();

    private JButton searchButton = new JButton(BtnOptEnum.SEARCH.getText());

    private JButton goButton = new JButton(BtnOptEnum.GO_OFFICIAL_WEBSITE.getText());

    private ZslCmdTable dataTable;

    private ZslRedisCmdHelpWindow(){

        this.setSize(W, H);

        this.setResizable(false);

        ZslRedisCmdHelpWindow me = this;

        this.keywordField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(KeyEvent.VK_ENTER == e.getExtendedKeyCode()){
                    me.dataTable.refreshTable(me.keywordField.getText(),me.groupBox.getSelectedItem() + "");
                }
            }
        });

        this.initTable();

        this.initSearchButton();

        this.initGoButton();

        this.initHelpWindow();



    }

    private void initTable() {
        this.dataTable = new ZslCmdTable(this.fontConfig, this);
    }

    private void initHelpWindow() {
        this.setTitle("Redis命令");
        this.setIconImage(IconUtils.getScaleImage(IconPaths.REDIS_CONSOLE_ICON, Constants.ICON_WIDTH , Constants.ICON_HEIGHT));

        this.setFont(FontUtils.getFont(this.fontConfig.getFontName(), Font.BOLD, this.fontConfig.getWindow()));


        int x = CommonUtils.maxWidth()/2 - W /2;
        int y = CommonUtils.maxHeight()/2 - H /2;

        this.setLocation(x, y);

        JPanel northPanel = new JPanel();
        northPanel.add(this.keywordField);
        northPanel.add(this.groupBox);
        northPanel.add(this.searchButton);
        northPanel.add(this.goButton);

        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.add(northPanel,BorderLayout.PAGE_START);
        contentPanel.add(new JScrollPane(this.dataTable), BorderLayout.CENTER);

        this.dataTable.setFillsViewportHeight(true);

        this.setContentPane(contentPanel);

        this.setVisible(true);
    }


    private void initGoButton() {
        this.goButton.addActionListener(e -> {
            try {
                Desktop.getDesktop().browse(new URI(COMMAND_URL));
            } catch (Exception ex) {
                //ignore
            }
        });
    }

    private void initSearchButton() {
        System.out.println(this.groupBox.getSelectedItem() + "");
        this.searchButton.addActionListener( e -> this.dataTable.refreshTable(this.keywordField.getText(), this.groupBox.getSelectedItem() + ""));
    }

    public static void showHelpDialog() {
        new ZslRedisCmdHelpWindow();
    }
}
